package com.atchangsha.system.service.impl;

import com.atchangsha.system.common.utils.Result;
import com.atchangsha.system.dao.OrderDao;
import com.atchangsha.system.entity.OrderEntity;
import com.atchangsha.system.service.IncomeService;
import com.atchangsha.system.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("incomeService")
public class IncomeServiceImpl implements IncomeService {
    @Resource
    private OrderDao orderDao;

    @Override
    public Result selectMoney() {
        //从数据库中查询出所有的已结算的订单
        List<OrderEntity> orderEntities = orderDao.selectList(new QueryWrapper<OrderEntity>().eq("remove_flag", 1));
        //准备一个map
        Map<Integer, List<Map<Integer, Double>>> map = new HashMap<>();
        //遍历每一个订单
        orderEntities.stream().forEach(item -> {
            Date time = item.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(time);
            //获取年份
            int year = Integer.valueOf(format.substring(0, 4));
            //获取月份
            int month = Integer.valueOf(format.substring(5, 7));
            //获取该订单收入
            double money = item.getMoney();
            //map中没有该年份的key，向map中放入key为该年份的元素，该元素的value为一个list，list里面装着各个月份和总收入
            if (!map.containsKey(year)) {
                map.put(year, new ArrayList<>());
            }
            //map里面有该年份，拿出所有的月份和总收入
            List<Map<Integer, Double>> list = map.get(year);
            //如果list的个数为0
            if (list.size() == 0) {
                for (int i = 1; i <= 13; i++) {
                    Map<Integer, Double> hashMap = new HashMap<>();
                    if (i == 13) {
                        //年收入
                        hashMap.put(i, money);
                    } else if (month == i) {
                        //月份入
                        hashMap.put(i, money);
                    } else {
                        //月收入
                        hashMap.put(i, 0.0);
                    }
                    list.add(hashMap);
                }
            } else {
                //list的个数不为0，找到对应的月份，进行月收入的增加
                list.stream().forEach(element -> {
                    if (element.containsKey(month)) {
                        Double monthSum = element.get(month);
                        element.put(month, monthSum + money);
                    }
                });
                //年收入也要增加
                Double yearSum = list.get(12).get(13);
                list.get(12).put(13, yearSum + money);
            }
        });
        //map转为list
        //TODO 转换可能出现性能问题
        ArrayList<Map.Entry<Integer, List<Map<Integer, Double>>>> list = new ArrayList<>(map.entrySet());
        //返回结果
        Map<String,Object> hashMap = new HashMap<>();
        hashMap.put("list",list);
        return Result.ok().setResult(hashMap);
    }
}
